package com.example.restatr.Atcivity.CitySubway;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.example.restatr.R;
import com.example.restatr.Tools;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class LostAndFoundAt extends AppCompatActivity {
    private Toolbar tb;
    private TextView barTitle;
    private EditText search;
    private ListView XiaoXiList;
    private SwipeRefreshLayout sx;
    private List<String> times = new ArrayList<>();
    private List<String> titles = new ArrayList<>();
    private List<String> xqs = new ArrayList<>();
    private List<String> imgUrl = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView( R.layout.activity_xiao_xi_at);


        tb = findViewById(R.id.toolbar);
        barTitle = findViewById(R.id.bar_Title);
        search = findViewById(R.id.search);
        XiaoXiList = findViewById(R.id.XiaoXiList);
        sx = findViewById(R.id.sx);

        Tools.SetBarTitle( tb,barTitle,"失物招领",search, LostAndFoundAt.this,false );


        new Thread(new Runnable() {
            @Override
            public void run() {
                final String get = Tools.Get("/prod-api/api/metro/found/list", "");
                if (get != null) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                JSONObject jx = new JSONObject(get);
                                JSONArray rows = jx.getJSONArray("data");
                                for (int i = 0; i < rows.length(); i++) {
                                    JSONObject all = rows.getJSONObject(i);
                                    JSONArray metroFoundList = all.getJSONArray("metroFoundList");
                                    JSONObject jsonObject = metroFoundList.getJSONObject(0);

                                    times.add(jsonObject.getString("foundTime"));
                                    titles.add(jsonObject.getString("foundPlace"));
                                    xqs.add(jsonObject.getString("claimPlace"));
                                    imgUrl.add(jsonObject.getString("imgUrl"));
                                    XiaoXiList.setAdapter(new XwAdapter());
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            }
        }).start();
    }

    class XwAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return times.size();
        }

        @Override
        public Object getItem(int position) {
            return times.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView=getLayoutInflater().inflate(R.layout.xiaoxi_list_item,null);
            TextView time =convertView.findViewById(R.id.time);
            TextView title =convertView.findViewById(R.id.title);
            TextView xq = convertView.findViewById(R.id.xq);
            ImageView tp = convertView.findViewById(R.id.tp);

            time.setText(times.get(position));
            title.setText(titles.get(position));
            xq.setText(xqs.get(position));
            Glide.with(LostAndFoundAt.this)
                    .load(Tools.ip+imgUrl.get(position))
                    .transform(new RoundedCorners(15))
                    .error(R.drawable.bj)
                    .into(tp);
            return convertView;
        }
    }
}